<template>
  <div class="app-container">
    <div style="width: 500px;margin-bottom: 20px;" v-if="showSearch">
      <div style="display: flex;align-items: center;flex-wrap: wrap;">
        <el-form ref="md5Ref" :model="form" :rules="rules" label-width="0px">
          <el-form-item label="" prop="value">
            <el-input :disabled="buttonLoading" v-model="form.value" placeholder="请输入加密内容" size="large"
              style="width: 500px;">
              <template #append>
                <el-button :disabled="buttonLoading" @click="submitForm">加密</el-button>
              </template>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['dataMd5:md5:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['dataMd5:md5:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="md5List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="dataMd5Id"/>
      <el-table-column label="小写" align="center" prop="smallMd5" />
      <el-table-column label="大写" align="center" prop="bigMd5" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['dataMd5:md5:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script setup name="Md5">
  import { listMd5, getMd5, delMd5, addMd5, updateMd5 } from "@/api/dataMd5/md5"

  const { proxy } = getCurrentInstance()

  const md5List = ref([])
  const open = ref(false)
  const buttonLoading = ref(false)
  const loading = ref(true)
  const showSearch = ref(true)
  const ids = ref([])
  const single = ref(true)
  const multiple = ref(true)
  const total = ref(0)
  const title = ref("")

  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
    },
    rules: {
      dataMd5Id: [
        { required: true, message: "不能为空", trigger: "blur" }
      ],
      smallMd5: [
        { required: true, message: "小写不能为空", trigger: "blur" }
      ],
      bigMd5: [
        { required: true, message: "大写不能为空", trigger: "blur" }
      ],
      value: [
        { required: true, message: "请输入加密内容", trigger: "blur" }
      ],
    }
  })

  const { queryParams, form, rules } = toRefs(data)

  /** 查询用户md5列列表 */
  function getList() {
    loading.value = true
    listMd5(queryParams.value).then(response => {
      md5List.value = response.rows
      total.value = response.total
      loading.value = false
    })
  }

  // 取消按钮
  function cancel() {
    open.value = false
    reset()
  }

  // 表单重置
  function reset() {
    form.value = {
      dataMd5Id: null,
      smallMd5: null,
      bigMd5: null,
    }
    proxy.resetForm("md5Ref")
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1
    getList()
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef")
    handleQuery()
  }

  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.dataMd5Id)
    single.value = selection.length != 1
    multiple.value = !selection.length
  }

  /** 新增按钮操作 */
  function handleAdd() {
    reset()
    open.value = true
    title.value = "添加用户md5列"
  }

  /** 修改按钮操作 */
  function handleUpdate(row) {
    loading.value = true
    reset()
    const _dataMd5Id = row.dataMd5Id || ids.value
    getMd5(_dataMd5Id).then(response => {
      loading.value = false
      form.value = response.data
      open.value = true
      title.value = "修改用户md5列"
    })
  }
  const strChange = (arg) => {
    var str = arg.split('')
    for (var i = 0; i < str.length; i++) {
      if (str[i].charAt() >= "a" && str[i].charAt() <= "z") {
        str[i] = str[i].toUpperCase()
        // console.log(str[i].toLowerCase());
      } else {
        str[i] = str[i].toLowerCase()
      }
    }
    return str.join('')
  }

  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["md5Ref"].validate(valid => {
      if (valid) {
        buttonLoading.value = true
        // form.value.smallMd5 = proxy.md5(form.value.value)
        // form.value.bigMd5 = strChange(proxy.md5(form.value.value))
        // delete form.value.value
        addMd5(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        }).finally(() => {
          buttonLoading.value = false
        })
        // if (form.value.dataMd5Id != null) {
        //   updateMd5(form.value).then(response => {
        //     proxy.$modal.msgSuccess("修改成功")
        //     open.value = false
        //     getList()
        //   }).finally(() => {
        //     buttonLoading.value = false
        //   })
        // } else {
        //   addMd5(form.value).then(response => {
        //     proxy.$modal.msgSuccess("新增成功")
        //     open.value = false
        //     getList()
        //   }).finally(() => {
        //     buttonLoading.value = false
        //   })
        // }
      }
    })
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const _dataMd5Ids = row.dataMd5Id || ids.value
    proxy.$modal.confirm('是否确认删除用户md5列编号为"' + _dataMd5Ids + '"的数据项？').then(function () {
      loading.value = true
      return delMd5(_dataMd5Ids)
    }).then(() => {
      loading.value = true
      getList()
      proxy.$modal.msgSuccess("删除成功")
    }).catch(() => {
    }).finally(() => {
      loading.value = false
    })
  }

  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('dataMd5/md5/export', {
      ...queryParams.value
    }, `md5_${new Date().getTime()}.xlsx`)
  }

  getList();
</script>
